/**
 * 
 */
package com.suyx.job.service.mh.impl;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.suyx.job.config.BaiduOCR;
import com.suyx.job.entity.DeviceInfo;
import com.suyx.job.entity.TuInfo;
import com.suyx.job.service.ITuInfoService;
import com.suyx.job.service.mh.MhxyService;
import com.suyx.job.utils.ImageUtils;
import com.suyx.job.utils.SampleOCRUtil;
import com.suyx.job.utils.ScreenCapture;
import com.suyx.job.utils.mh.AlgScene;
import com.suyx.job.utils.mh.BjlzScene;
import com.suyx.job.utils.mh.CsjwScene;
import com.suyx.job.utils.mh.DhwScene;
import com.suyx.job.utils.mh.DtgjScene;
import com.suyx.job.utils.mh.DtjwScene;
import com.suyx.job.utils.mh.HgsScene;
import com.suyx.job.utils.mh.JnywScene;
import com.suyx.job.utils.mh.JycScene;
import com.suyx.job.utils.mh.MjcScene;
import com.suyx.job.utils.mh.NecScene;
import com.suyx.job.utils.mh.PtsScene;
import com.suyx.job.utils.mh.QlsScene;
import com.suyx.job.utils.mh.StlScene;
import com.suyx.job.utils.mh.WzgScene;
import com.suyx.job.utils.mh.ZzgScene;

import lombok.extern.slf4j.Slf4j;

/**
 * @author Administrator
 *
 */
@Slf4j
@Service
public class MhxyServiceImpl implements MhxyService {

	@Autowired
	private AlgScene algScene;
	@Autowired
	private BjlzScene bjlzScene;
	@Autowired
	private CsjwScene csjwScene;
	@Autowired 
	private DhwScene dhwScene;
	@Autowired
	private DtgjScene dtgjScene;
	@Autowired
	private DtjwScene dtjwScene;
	@Autowired
	private HgsScene hgsScene;
	@Autowired
	private JnywScene jnywScene;
	@Autowired
	private JycScene jycScene;
	@Autowired
	private MjcScene mjcScene;
	@Autowired
	private NecScene necScene;
	@Autowired
	private PtsScene ptsScene;
	@Autowired
	private QlsScene qlsScene;
	@Autowired
	private StlScene stlScene;
	@Autowired
	private WzgScene wzgScene;
	@Autowired
	private ZzgScene zzgScene;
	@Autowired
    private BaiduOCR baiduOCR;
	@Autowired
	private ITuInfoService tuInfoService;
	private static String SC_PIC_PATH = "H:\\android\\MH\\";//梦幻宝图截图放置路径
	private static final List<String> bgzb1 = Arrays.asList("730 224", "809 212", "906 233", "1000 224", "1084 224",
	          "730 321", "811 324", "909 315", "1000 320", "1080 324",
	          "724 408", "812 408", "909 408", "1000 408", "1080 408",
	          "718 492", "812 492", "909 492", "1000 492", "1080 492");

    private static final List<String> bgzb = Arrays.asList("909 315", "1000 320", "1080 324",
	         "724 408", "812 408", "909 408", "1000 408", "1080 408",
	         "718 492", "812 492", "909 492", "1000 492", "1080 492");



	/* (non-Javadoc)
	 * @see com.suyx.job.service.mh.MhxyService#playBaoTu(com.suyx.job.entity.BaoTu)
	 */
	@Override
	public void playBaoTu(TuInfo bt,DeviceInfo deviceInfo) {
		String path = SC_PIC_PATH + bt.getDevice() +"\\";
		/*try {
			String beforeBgZuobiao = null;
			File file = new File(path+bt.getDevice() +".png");
			if(file.exists()){
				beforeBgZuobiao = baiduOCR.generalBasic(path+bt.getDevice() +".png");
			}
			
			if(bt.getScene().indexOf("东海") != -1){
				log.info("beforeBgZuobiao:" + beforeBgZuobiao);
				if(org.apache.commons.lang3.StringUtils.isEmpty(beforeBgZuobiao)
						|| beforeBgZuobiao.indexOf(bt.getScene()) == -1){//查看当前挖图是否和上一张一个场景
					dhwScene.dhw(bt.getDevice(),bt.getZuobiaoX()+" "+bt.getZuobiaoY(),bt.getLocationX()+"  "+bt.getLocationY());
				}
				dhwScene.dhwSceneSame(bt.getDevice(),bt.getZuobiaoX()+" "+bt.getZuobiaoY(),bt.getLocationX()+"  "+bt.getLocationY());
				log.info("东海湾挖图结束..");
			}
			
			if(bt.getScene().indexOf("长寿郊外") != -1 || bt.getScene().indexOf("长寿") != -1){
				log.info("beforeBgZuobiao:"+beforeBgZuobiao);
				if(org.apache.commons.lang3.StringUtils.isEmpty(beforeBgZuobiao)
						|| beforeBgZuobiao.indexOf("长寿") == -1){//查看当前挖图是否和上一张一个场景
					csjwScene.csjw(bt.getDevice(),bt.getZuobiaoX()+" "+bt.getZuobiaoY(),bt.getLocationX()+"  "+bt.getLocationY());
				}
				csjwScene.csjwSceneSame(bt.getDevice(),bt.getZuobiaoX()+" "+bt.getZuobiaoY(),bt.getLocationX()+"  "+bt.getLocationY());
				log.info("长寿郊外挖图结束..");
			}
			
			if(bt.getScene().indexOf("大唐国境") != -1 || bt.getScene().indexOf("国境")  != -1 ){
				log.info("beforeBgZuobiao:"+beforeBgZuobiao);
				if(org.apache.commons.lang3.StringUtils.isEmpty(beforeBgZuobiao)
						|| beforeBgZuobiao.indexOf(bt.getScene()) == -1){//查看当前挖图是否和上一张一个场景
					dtgjScene.dtgj(bt.getDevice(),bt.getZuobiaoX()+" "+bt.getZuobiaoY(),bt.getLocationX()+"  "+bt.getLocationY());
				}
				dtgjScene.dtgjSceneSame(bt.getDevice(),bt.getZuobiaoX()+" "+bt.getZuobiaoY(),bt.getLocationX()+"  "+bt.getLocationY());
				log.info("大唐国境挖图结束..");
			}
			
			if(bt.getScene().indexOf("大唐境外") != -1 || bt.getScene().indexOf("境外") != -1 ){
				log.info("beforeBgZuobiao:"+beforeBgZuobiao);
				if(org.apache.commons.lang3.StringUtils.isEmpty(beforeBgZuobiao)
						|| beforeBgZuobiao.indexOf("境外") == -1){//查看当前挖图是否和上一张一个场景
					dtjwScene.dtjw(bt.getDevice(),bt.getZuobiaoX()+" "+bt.getZuobiaoY(),bt.getLocationX()+"  "+bt.getLocationY());
				}
				dtjwScene.dtjwSceneSame(bt.getDevice(),bt.getZuobiaoX()+" "+bt.getZuobiaoY(),bt.getLocationX()+"  "+bt.getLocationY());
				log.info("大唐境外挖图结束..");
			}
			if(bt.getScene().indexOf("江南野外") != -1 
					|| bt.getScene().indexOf("江南") != -1
					|| bt.getScene().indexOf("江甬野列") != -1){
				log.info("beforeBgZuobiao:"+beforeBgZuobiao);
				if(org.apache.commons.lang3.StringUtils.isEmpty(beforeBgZuobiao)
						|| beforeBgZuobiao.indexOf("江") == -1){//查看当前挖图是否和上一张一个场景
					jnywScene.jnyw(bt.getDevice(),bt.getZuobiaoX()+" "+bt.getZuobiaoY(),bt.getLocationX()+"  "+bt.getLocationY());
				}
				jnywScene.jnywSceneSame(bt.getDevice(),bt.getZuobiaoX()+" "+bt.getZuobiaoY(),bt.getLocationX()+"  "+bt.getLocationY());
				log.info("江南野外挖图结束..");
			}
			if(bt.getScene().indexOf("来国") != -1 || bt.getScene().indexOf("傲来国") != -1 ){
				log.info("beforeBgZuobiao:"+beforeBgZuobiao);
				if(org.apache.commons.lang3.StringUtils.isEmpty(beforeBgZuobiao)
						|| beforeBgZuobiao.indexOf("来国") == -1){
					//查看当前挖图是否和上一张一个场景
					algScene.alg(bt.getDevice(),bt.getZuobiaoX()+" "+bt.getZuobiaoY(),bt.getLocationX()+"  "+bt.getLocationY());
				}
				algScene.algSceneSame(bt.getDevice(),bt.getZuobiaoX()+" "+bt.getZuobiaoY(),bt.getLocationX()+"  "+bt.getLocationY());
				log.info("傲来国挖图结束..");
			}
			if( bt.getScene().indexOf("墨家") != -1 
					||bt.getScene().indexOf("家村") != -1){
				log.info("beforeBgZuobiao:"+beforeBgZuobiao);
				if(org.apache.commons.lang3.StringUtils.isEmpty(beforeBgZuobiao)
						|| beforeBgZuobiao.indexOf("家村") == -1
						|| beforeBgZuobiao.indexOf("冢") == -1){//查看当前挖图是否和上一张一个场景
					mjcScene.mjc(bt.getDevice(),bt.getZuobiaoX()+" "+bt.getZuobiaoY(),bt.getLocationX()+"  "+bt.getLocationY());
				}
				mjcScene.mjcSceneSame(bt.getDevice(),bt.getZuobiaoX()+" "+bt.getZuobiaoY(),bt.getLocationX()+"  "+bt.getLocationY());
				log.info("墨家村挖图结束..");
			}
			if(bt.getScene().indexOf("普陀山") != -1 || bt.getScene().indexOf("普") != -1 ){
				log.info("beforeBgZuobiao:"+beforeBgZuobiao);
				if(org.apache.commons.lang3.StringUtils.isEmpty(beforeBgZuobiao)
						|| beforeBgZuobiao.indexOf(bt.getScene()) == -1){//查看当前挖图是否和上一张一个场景
					if("pts".equals(deviceInfo.getRemark2())){
						ptsScene.ptsCs(bt.getDevice());
					}else{
						ptsScene.pts(bt.getDevice(),bt.getZuobiaoX()+" "+bt.getZuobiaoY(),bt.getLocationX()+"  "+bt.getLocationY());
					}
					
				}
				ptsScene.ptsSceneSame(bt.getDevice(),bt.getZuobiaoX()+" "+bt.getZuobiaoY(),bt.getLocationX()+"  "+bt.getLocationY());
				log.info("普陀山挖图结束..");
			}
			
			if(bt.getScene().indexOf("狮驼岭") != -1 || bt.getScene().indexOf("狮") != -1 ){
				log.info("beforeBgZuobiao:"+beforeBgZuobiao);
				if(org.apache.commons.lang3.StringUtils.isEmpty(beforeBgZuobiao)
						|| beforeBgZuobiao.indexOf(bt.getScene()) == -1){//查看当前挖图是否和上一张一个场景
					stlScene.stl(bt.getDevice(),bt.getZuobiaoX()+" "+bt.getZuobiaoY(),bt.getLocationX()+"  "+bt.getLocationY());
				}
				stlScene.stlSceneSame(bt.getDevice(),bt.getZuobiaoX()+" "+bt.getZuobiaoY(),bt.getLocationX()+"  "+bt.getLocationY());
				log.info("狮驼岭挖图结束..");
			}
			if(bt.getScene().indexOf("五") != -1 ){
				log.info("beforeBgZuobiao:"+beforeBgZuobiao);
				if(org.apache.commons.lang3.StringUtils.isEmpty(beforeBgZuobiao)
						|| beforeBgZuobiao.indexOf(bt.getScene()) == -1){//查看当前挖图是否和上一张一个场景
					wzgScene.wzg(bt.getDevice(),bt.getZuobiaoX()+" "+bt.getZuobiaoY(),bt.getLocationX()+"  "+bt.getLocationY());
				}
				wzgScene.wzgSceneSame(bt.getDevice(),bt.getZuobiaoX()+" "+bt.getZuobiaoY(),bt.getLocationX()+"  "+bt.getLocationY());
				log.info("五庄观挖图结束..");
			}
			if(bt.getScene().indexOf("朱紫国") != -1 || bt.getScene().indexOf("朱紫") != -1 ){
				log.info("beforeBgZuobiao:"+beforeBgZuobiao);
				if(org.apache.commons.lang3.StringUtils.isEmpty(beforeBgZuobiao)
						|| beforeBgZuobiao.indexOf(bt.getScene()) == -1){//查看当前挖图是否和上一张一个场景
					zzgScene.zzg(bt.getDevice(),bt.getZuobiaoX()+" "+bt.getZuobiaoY(),bt.getLocationX()+"  "+bt.getLocationY());
				}
				zzgScene.zzgSceneSame(bt.getDevice(),bt.getZuobiaoX()+" "+bt.getZuobiaoY(),bt.getLocationX()+"  "+bt.getLocationY());
				log.info("朱紫国挖图结束..");
			}
			if(bt.getScene().indexOf("麒麟山") != -1 || bt.getScene().indexOf("麒") != -1 ){
				log.info("beforeBgZuobiao:"+beforeBgZuobiao);
				if(org.apache.commons.lang3.StringUtils.isEmpty(beforeBgZuobiao)
						|| beforeBgZuobiao.indexOf(bt.getScene()) == -1){//查看当前挖图是否和上一张一个场景
					qlsScene.qls(bt.getDevice(),bt.getZuobiaoX()+" "+bt.getZuobiaoY(),bt.getLocationX()+"  "+bt.getLocationY());
				}
				qlsScene.qlsSceneSame(bt.getDevice(),bt.getZuobiaoX()+" "+bt.getZuobiaoY(),bt.getLocationX()+"  "+bt.getLocationY());
				log.info("麒麟山挖图结束..");
			}
			if(bt.getScene().indexOf("花果山") != -1 || bt.getScene().indexOf("果山") != -1 ){
				log.info("beforeBgZuobiao:"+beforeBgZuobiao);
				if(org.apache.commons.lang3.StringUtils.isEmpty(beforeBgZuobiao)
						|| beforeBgZuobiao.indexOf(bt.getScene()) == -1){//查看当前挖图是否和上一张一个场景
					hgsScene.hgs(bt.getDevice(),bt.getZuobiaoX()+" "+bt.getZuobiaoY(),bt.getLocationX()+"  "+bt.getLocationY());
				}
				hgsScene.hgsSceneSame(bt.getDevice(),bt.getZuobiaoX()+" "+bt.getZuobiaoY(),bt.getLocationX()+"  "+bt.getLocationY());
				log.info("花果山挖图结束..");
			}
			
			if(bt.getScene().indexOf("建邺城") != -1 || bt.getScene().indexOf("建") != -1 ){
				log.info("beforeBgZuobiao:"+beforeBgZuobiao);
				if(org.apache.commons.lang3.StringUtils.isEmpty(beforeBgZuobiao)
						|| beforeBgZuobiao.indexOf("建") == -1){//查看当前挖图是否和上一张一个场景
					jycScene.jyc(bt.getDevice(),bt.getZuobiaoX()+" "+bt.getZuobiaoY(),bt.getLocationX()+"  "+bt.getLocationY());
				}
				jycScene.jycSceneSame(bt.getDevice(),bt.getZuobiaoX()+" "+bt.getZuobiaoY(),bt.getLocationX()+"  "+bt.getLocationY());
				log.info("建邺城挖图结束..");
			}
			
			if(bt.getScene().indexOf("女儿村") != -1 || bt.getScene().indexOf("女儿") != -1 ){
				log.info("beforeBgZuobiao:"+beforeBgZuobiao);
				if(org.apache.commons.lang3.StringUtils.isEmpty(beforeBgZuobiao)
						|| beforeBgZuobiao.indexOf(bt.getScene()) == -1){//查看当前挖图是否和上一张一个场景
					necScene.nec(bt.getDevice(),bt.getZuobiaoX()+" "+bt.getZuobiaoY(),bt.getLocationX()+"  "+bt.getLocationY());
				}
				necScene.necSceneSame(bt.getDevice(),bt.getZuobiaoX()+" "+bt.getZuobiaoY(),bt.getLocationX()+"  "+bt.getLocationY());
				log.info("女儿村挖图结束..");
			}
			
			if(bt.getScene().indexOf("北俱") != -1 || bt.getScene().indexOf("北") != -1 ){
				log.info("beforeBgZuobiao:"+beforeBgZuobiao);
				if(org.apache.commons.lang3.StringUtils.isEmpty(beforeBgZuobiao)
						|| beforeBgZuobiao.indexOf("北俱") == -1){//查看当前挖图是否和上一张一个场景
					bjlzScene.bjlz(bt.getDevice(),bt.getZuobiaoX()+" "+bt.getZuobiaoY(),bt.getLocationX()+"  "+bt.getLocationY());
				}
				bjlzScene.bjlzSceneSame(bt.getDevice(),bt.getZuobiaoX()+" "+bt.getZuobiaoY(),bt.getLocationX()+"  "+bt.getLocationY());
				log.info("北俱芦洲挖图结束..");
			}
			tuInfoService.deleteById(bt.getTuId());
		} catch (Exception e) {
			log.error("挖图异常："+e.getMessage());
		}*/
		
	}

	@Override
	public List<TuInfo> getBaoTuInfo(String device,List<String> bgzb) {
		try {
			double start = System.currentTimeMillis();
			List<TuInfo> baotuList = new ArrayList<>();
			/*double yanTime = 0;
			//if(ds.length>0) {
				//打开包裹
				ScreenCapture.mouseClick(device, "1308 675");
				ScreenCapture.mouseClick(device, "1084 224");//摄妖香
				ScreenCapture.mouseClick(device, "522 422");// 使用
				TuInfo btInfo = null;
				for(String bg : bgzb){
					double end = System.currentTimeMillis();
					System.out.println("摄妖香消耗："+(end-start)/1000+" s");
					yanTime = (end-start)/1000 ;
					if(yanTime > 1500){
						ScreenCapture.mouseClick(device, "1084 224");//摄妖香
						ScreenCapture.mouseClick(device, "522 422");// 使用
						yanTime = 0;
						start = System.currentTimeMillis();
					}
					ScreenCapture.mouseClick(device, bg);
					//ScreenCapture.mouseClick(device, "522 422");// 使用
					Thread.sleep(1 * 500L);
					//ScreenCapture.mouseClick(device, bg);
					ScreenCapture.snapshot(device,"H:\\android\\imges2\\",device + "_" +bg.replace(" ", "_") + ".png");
					//剪切到宝图坐标
					ImageUtils.cutPNG(new FileInputStream("H:\\android\\imges2\\"+device + "_" +bg.replace(" ", "_")+".png"),
	                           new FileOutputStream("H:\\android\\imges2\\tuaddr\\"+device + "_" +bg.replace(" ", "_")+"_btAddr.png"), 375,335,232,36);
					//读取宝图坐标地址信息
					String bgZuobiao = SampleOCRUtil.sampleBasic("H:\\android\\imges2\\tuaddr\\"+device + "_" +bg.replace(" ", "_")+"_btAddr.png");
					if(StringUtils.isEmpty(bgZuobiao)){
						System.out.println("读取宝图坐标地址信息为空："+device + "_" +bg.replace(" ", "_")+"_btAddr.png");
						continue;
					}
					String[] zb = bgZuobiao.replace(" ", "").split("\\(");
					if(zb.length ==2){
						btInfo = new TuInfo();
						btInfo.setDevice(device);
						btInfo.setScene(zb[0]);// 场景
						btInfo.setLocationX(Integer.valueOf(bg.split(" ")[0]));//包裹位置x
						btInfo.setLocationY(Integer.valueOf(bg.split(" ")[1]));//包裹位置y
						String[] z = zb[1].replace(".", ",").replace(")", "").split(",");
						if(z.length == 2){
							btInfo.setZuobiaoX(Integer.valueOf(z[0]));
							btInfo.setZuobiaoY(Integer.valueOf(z[1]));
							btInfo.setRemark(bgZuobiao);
							baotuList.add(btInfo);
						}else{
							System.out.println("读取宝图坐标地址信息有误决定掉（精密接口）："+bgZuobiao);
							String bgZuobiao0 = SampleOCRUtil.sampleAccurate("H:\\android\\imges2\\tuaddr\\"+device + "_" +bg.replace(" ", "_")+"_btAddr.png");
							if(StringUtils.isEmpty(bgZuobiao0)){
								System.out.println("（精密接口）读取宝图坐标地址信息为空："+device + "_" +bg.replace(" ", "_")+"_btAddr.png");
								continue;
							}
							String[] zb1 = bgZuobiao0.replace(" ", "").split("\\(");
							if(zb1.length ==2){
								btInfo = new TuInfo();
								btInfo.setDevice(device);
								btInfo.setScene(zb1[0]);// 场景
								btInfo.setLocationX(Integer.valueOf(bg.split(" ")[0]));//包裹位置x
								btInfo.setLocationY(Integer.valueOf(bg.split(" ")[1]));//包裹位置y
								String[] z1 = zb1[1].replace(".", ",").replace(")", "").split(",");
								if(z1.length == 2){
									btInfo.setZuobiaoX(Integer.valueOf(z[0]));
									btInfo.setZuobiaoY(Integer.valueOf(z[1]));
									btInfo.setRemark(bgZuobiao);
									baotuList.add(btInfo);
								}else{
									System.out.println("（精密接口）读取宝图坐标地址信息有误决："+bgZuobiao);
									continue;
								}
							}else{
								System.out.println("精密接口）读取宝图坐标地址信息有误："+bgZuobiao);
								continue;
							}
						}
					}else{
						System.out.println("读取宝图坐标地址信息有误："+bgZuobiao);
						continue;
					}
					System.out.println(bgZuobiao);
				}*/
			ScreenCapture.mouseClick(device, "1156 52");// 关闭包裹
			return baotuList;
		} catch (Exception e) {
			log.info("读取包裹宝图信息异常："+e.getMessage());
			return null;
		}
	}

	@Override
	public void startPlayDaTu(String deviceCode, String bg) {
		/*BaoTu bt = getBaoTuInfo(deviceCode,bg);
		if(bt != null){
			playBaoTu(bt);
		}*/
		
	}

}
